package oj_qmfx.oj_22;

import java.util.Scanner;

public class Main {
    private static int[][] edge;    //邻接矩阵
    private static boolean[] visited;   //访问标记数组
    private static int n;   //顶点数量
    private static int m;   //边数量

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();

        edge = new int[n][n];
        visited = new boolean[n];

        //构建图
        for (int i = 0; i < m; i++){
            int x = sc.nextInt();
            int y = sc.nextInt();
//            x 到 y 和 y 到 x 都有边
            edge[x][y] = 1;
            edge[y][x] = 1;
        }

        DFS(0);
        sc.close();
    }


    public static void DFS(int x){
//        先标记为访问过,再输出该顶点
        visited[x] = true;
        System.out.print(x + ",");

//        找相邻节点
        for (int y = 0; y < n; y++){
//            判断条件：1.两点间必须有一条边    2.该点未被访问过
            if (edge[x][y] == 1 && visited[y] == false){
//                满足条件 继续搜索
                DFS(y);
            }
        }
    }
}
